package gui;

import gui.AbstractViewPane.ViewMode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import business.TypeHeureBL;

public class PanelHeureUIConsulter extends JFrame {
	
	private ViewMode currentViewMode;
	protected JFrame masterClass = this;
	
	//champs du formulaire
	private JPanel general;
    private JButton save;
    private JButton annuler;
	JTextField textFieldNom;

  //variables d'affichage du formulaire
    private int id = -1;
    private String nom = "";
    
    private PanelHeureUI panelSource;
	
	public PanelHeureUIConsulter(ViewMode v, int idTypeHeure, PanelHeureUI panelS) {
		
		TypeHeureBL typeHeureBL = new TypeHeureBL();
		currentViewMode = v;
		panelSource = panelS;
		
    	setSize(500, 300);
		setTitle("Gestion des types d'heure");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder(getTitle()),
		BorderFactory.createEmptyBorder(5,5,5,5)));

		// si l'id du noeud envoyé est -1, on est en mode ajout, les champs
		// seront donc vides
		if (idTypeHeure == -1) {
			nom = "";
			id = -1;
		} else {
			// si on est en consultation ou modification (récuperation des
			// elements en base)
			id = idTypeHeure;
			typeHeureBL = new TypeHeureBL(id);
			nom = typeHeureBL.getNomTypeHeure();
		}
		
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
        JLabel labelNom = new JLabel("Nom:", JLabel.TRAILING);
        p.add(labelNom);
        textFieldNom = new JTextField (nom);
        labelNom.setLabelFor(textFieldNom);
        p.add(textFieldNom);
        
        //Lay out the panel
        SpringUtilities.makeCompactGrid(p,
                                        1, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new PanelHeureListener());
        commandBox.add(save);
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new PanelHeureListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);

	}

	//methodes de récupperation des éléments des champs
	public int getId(){
		return id;
	}
	public String getNom(){
		return textFieldNom.getText();
	}
    
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	private class PanelHeureListener implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
			System.out.println("Action");		
			if (e.getActionCommand().equals("Enregistrer")) {
				System.out.println("Demande d'enregistrement");
				TypeHeureBL typeSave = new TypeHeureBL();
				ArrayList<String> listeErreurs;
				// si -1, c'est un ajout d'un type heure, sinon modification du
				// type heure de l'id concerné
				if (id == -1) {
					listeErreurs = typeSave.ajouterTypeHeure(getId(), getNom());
					if (! listeErreurs.isEmpty()) {
						new MessageErrorUI(masterClass, listeErreurs);
					}
					else dispose();
				} else {
					listeErreurs = typeSave.modifierTypeHeure(getId(), getNom());
					if (! listeErreurs.isEmpty()) {
						new MessageErrorUI(masterClass, listeErreurs);
					}
					else dispose();
				}
				panelSource.refreshListe();
			} else if (e.getActionCommand().equals("Annuler")) {
				System.out.println("annulation");
				dispose();
			}
		}
	}
}
